package com.ymatou.query.trading.api.testcase;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Map;
import java.util.Random;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.query.trading.api.parameter.GetOrderPostPayInfoBean;
import com.ymatou.query.trading.api.service.GetOrderPostPayInfoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.utils.constant.ECode;

/**
 * 获取买家补款信息 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_GetOrderPostPayInfo {

	private static GetOrderPostPayInfoBean getorderpostpayinfobean;
	private static GetOrderPostPayInfoCall getorderpostpayinfocall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取买家补款信息");
	}

	@Before
	public void caseUp() {

		getorderpostpayinfobean = new GetOrderPostPayInfoBean();
		getorderpostpayinfocall = new GetOrderPostPayInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getorderpostpayinfocall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderPostPayInfo_001() {
		Logger.start(true,"获取买家补款信息成功");
		try {
			// 创建订单
			Map map = new OrderCallService().appSaveOrder();		
			int orderId = (Integer) map.get("orderId");
			int sellerId = (Integer) map.get("sellerId");
			int userId = (Integer) map.get("userId");
			int tradingId = (Integer) map.get("tradingId");
			DecimalFormat df = new DecimalFormat("0.00");// 保留两位小数
			BigDecimal totalPrice = (BigDecimal) map.get("totalPrice");// 支付金额
			// 买家付款
			new OrderCallService().orderPayRequest(userId, totalPrice, tradingId);
			// 接单
			new OrderCallService().acceptOrder(orderId, sellerId);
			// 卖家发起补款
			int price = 1+new Random().nextInt(50);
			BigDecimal payAmount = new BigDecimal(df.format(price));
			String postPayId = new OrderCallService().applyForPostPay(orderId, sellerId, payAmount);
			// 参数
			getorderpostpayinfobean.setBuyerId(userId);
			getorderpostpayinfobean.setOrderId(orderId);
			// 发送请求
			getorderpostpayinfocall.setData(getorderpostpayinfobean);
			getorderpostpayinfocall.callService();
			Logger.comment("getorderpostpayinfocall send:" + getorderpostpayinfocall.getEntityString());
			Logger.comment("getorderpostpayinfocall response:" + getorderpostpayinfocall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, getorderpostpayinfocall.getCode(), "验证Code");
			// Logger.verifyEquals(ErrorCode.SUCCESSMSG,
			// getorderpostpayinfocall.getMsg(), "验证Msg");
			Logger.verifyEquals(true, getorderpostpayinfocall.getAvailAmount() > 0, "验证AvailAmount");
			Logger.verifyEquals(true, getorderpostpayinfocall.getGiftAvailAmount() >= 0, "验证红包最大使用数量");
			Logger.verifyEquals(true, getorderpostpayinfocall.getExistsTradingPassword(), "验证用户是否设置交易密码");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}